package com.uzai.console.service.tb.tbuserinfo.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.TbUserInfo.TbUserInfoQuery;
import com.uzai.console.dto.TbUserInfo.TbUserInfoUpdateDto;
import com.uzai.console.dto.remoteapi.DirectionalGoodsQuery;
import com.uzai.console.entity.TbToken;
import com.uzai.console.entity.TbUserInfo;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.mapper.TbTokenMapper;
import com.uzai.console.mapper.TbUserInfoMapper;
import com.uzai.console.service.remoteapi.TlbRemoteApiService;
import com.uzai.console.service.tb.tbuserinfo.TbUserInfoService;
import com.uzai.console.vo.remoteapi.cps.DirectionalGoods;
import com.uzai.console.vo.tbuserinfo.TbUserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
@Slf4j
public class TbUserInfoServiceImpl implements TbUserInfoService {

    @Autowired
    private TbUserInfoMapper tbUserInfoMapper;
    @Autowired
    private TbTokenMapper tbTokenMapper;

    /**
     * 分页查询推老板淘宝定向商品列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return TbExclusiveGoodsVo
     */
    public Page<TbUserInfoVo> findTbUserInfoList(TbUserInfoQuery tbUserInfoQuery){
        //新建返回到页面的LIST对象
        List<TbUserInfoVo> tbUserInfoVoList = new ArrayList<TbUserInfoVo>();
        //执行分页查询对象
        Page<TbUserInfo> page = new Page<>();
        tbUserInfoQuery.convert(page);
        List<TbUserInfo> tbUserInfoList = tbUserInfoMapper.findByList(page, tbUserInfoQuery);
        if(tbUserInfoList != null){
            for (TbUserInfo tbUserInfo : tbUserInfoList){
                TbUserInfoVo tbUserInfoVo = new  TbUserInfoVo();
                BeanUtils.copyProperties(tbUserInfo, tbUserInfoVo);
                //用户昵称
                TbToken tbToken = tbTokenMapper.selectByUserIdAndTurnLink(tbUserInfo.getTbTokenId(),0);
                if(tbToken == null){
                    tbToken = tbTokenMapper.selectByUserIdAndTurnLink(tbUserInfo.getTbTokenId(), 1);
                }
                if(tbToken != null){
                    tbUserInfoVo.setTbTokenName(tbToken.getUserName());
                }
                tbUserInfoVoList.add(tbUserInfoVo);
            }
        }
        //返回到页面分页对象
        Page<TbUserInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(tbUserInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 修改会员渠道ID
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param tbUserInfoUpdateDto
     * @return null
     */
    public void updateTbUserInfo(TbUserInfoUpdateDto tbUserInfoUpdateDto){
        if(tbUserInfoUpdateDto.getId() == null || StringUtils.isBlank(tbUserInfoUpdateDto.getSpecialId())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "主键ID不能为空");
        }
        TbUserInfo tbUserInfo = tbUserInfoMapper.selectById(tbUserInfoUpdateDto.getId(), tbUserInfoUpdateDto.getMerId());

        if(tbUserInfo == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此记录不存在");
        }

        tbUserInfo.setSpecialId(tbUserInfoUpdateDto.getSpecialId());
        tbUserInfo.setRelationId(tbUserInfoUpdateDto.getRelationId());
        tbUserInfo.setUpdateTime(DateUtil.getNowTime());
        tbUserInfoMapper.updateById(tbUserInfo);
    }



}
